/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controladores;

import controladores.exceptions.NonexistentEntityException;
import controladores.exceptions.PreexistingEntityException;
import entidades.Solicitudinsumo;
import entidades.SolicitudinsumoPK;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entidades.Solicitud;
import entidades.Insumo;

/**
 *
 * @author Andres
 */
public class SolicitudinsumoJpaController {

    public SolicitudinsumoJpaController() {
        emf = Persistence.createEntityManagerFactory("proyectoCongresoPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Solicitudinsumo solicitudinsumo) throws PreexistingEntityException, Exception {
        if (solicitudinsumo.getSolicitudinsumoPK() == null) {
            solicitudinsumo.setSolicitudinsumoPK(new SolicitudinsumoPK());
        }
        solicitudinsumo.getSolicitudinsumoPK().setInsumoIdinsumo(solicitudinsumo.getInsumo().getIdinsumo());
        solicitudinsumo.getSolicitudinsumoPK().setSolicitudIdsolicitud(solicitudinsumo.getSolicitud().getIdsolicitud());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Solicitud solicitud = solicitudinsumo.getSolicitud();
            if (solicitud != null) {
                solicitud = em.getReference(solicitud.getClass(), solicitud.getIdsolicitud());
                solicitudinsumo.setSolicitud(solicitud);
            }
            Insumo insumo = solicitudinsumo.getInsumo();
            if (insumo != null) {
                insumo = em.getReference(insumo.getClass(), insumo.getIdinsumo());
                solicitudinsumo.setInsumo(insumo);
            }
            em.persist(solicitudinsumo);
            if (solicitud != null) {
                solicitud.getSolicitudinsumoList().add(solicitudinsumo);
                solicitud = em.merge(solicitud);
            }
            if (insumo != null) {
                insumo.getSolicitudinsumoList().add(solicitudinsumo);
                insumo = em.merge(insumo);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findSolicitudinsumo(solicitudinsumo.getSolicitudinsumoPK()) != null) {
                throw new PreexistingEntityException("Solicitudinsumo " + solicitudinsumo + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Solicitudinsumo solicitudinsumo) throws NonexistentEntityException, Exception {
        solicitudinsumo.getSolicitudinsumoPK().setInsumoIdinsumo(solicitudinsumo.getInsumo().getIdinsumo());
        solicitudinsumo.getSolicitudinsumoPK().setSolicitudIdsolicitud(solicitudinsumo.getSolicitud().getIdsolicitud());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Solicitudinsumo persistentSolicitudinsumo = em.find(Solicitudinsumo.class, solicitudinsumo.getSolicitudinsumoPK());
            Solicitud solicitudOld = persistentSolicitudinsumo.getSolicitud();
            Solicitud solicitudNew = solicitudinsumo.getSolicitud();
            Insumo insumoOld = persistentSolicitudinsumo.getInsumo();
            Insumo insumoNew = solicitudinsumo.getInsumo();
            if (solicitudNew != null) {
                solicitudNew = em.getReference(solicitudNew.getClass(), solicitudNew.getIdsolicitud());
                solicitudinsumo.setSolicitud(solicitudNew);
            }
            if (insumoNew != null) {
                insumoNew = em.getReference(insumoNew.getClass(), insumoNew.getIdinsumo());
                solicitudinsumo.setInsumo(insumoNew);
            }
            solicitudinsumo = em.merge(solicitudinsumo);
            if (solicitudOld != null && !solicitudOld.equals(solicitudNew)) {
                solicitudOld.getSolicitudinsumoList().remove(solicitudinsumo);
                solicitudOld = em.merge(solicitudOld);
            }
            if (solicitudNew != null && !solicitudNew.equals(solicitudOld)) {
                solicitudNew.getSolicitudinsumoList().add(solicitudinsumo);
                solicitudNew = em.merge(solicitudNew);
            }
            if (insumoOld != null && !insumoOld.equals(insumoNew)) {
                insumoOld.getSolicitudinsumoList().remove(solicitudinsumo);
                insumoOld = em.merge(insumoOld);
            }
            if (insumoNew != null && !insumoNew.equals(insumoOld)) {
                insumoNew.getSolicitudinsumoList().add(solicitudinsumo);
                insumoNew = em.merge(insumoNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                SolicitudinsumoPK id = solicitudinsumo.getSolicitudinsumoPK();
                if (findSolicitudinsumo(id) == null) {
                    throw new NonexistentEntityException("The solicitudinsumo with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(SolicitudinsumoPK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Solicitudinsumo solicitudinsumo;
            try {
                solicitudinsumo = em.getReference(Solicitudinsumo.class, id);
                solicitudinsumo.getSolicitudinsumoPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The solicitudinsumo with id " + id + " no longer exists.", enfe);
            }
            Solicitud solicitud = solicitudinsumo.getSolicitud();
            if (solicitud != null) {
                solicitud.getSolicitudinsumoList().remove(solicitudinsumo);
                solicitud = em.merge(solicitud);
            }
            Insumo insumo = solicitudinsumo.getInsumo();
            if (insumo != null) {
                insumo.getSolicitudinsumoList().remove(solicitudinsumo);
                insumo = em.merge(insumo);
            }
            em.remove(solicitudinsumo);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Solicitudinsumo> findSolicitudinsumoEntities() {
        return findSolicitudinsumoEntities(true, -1, -1);
    }

    public List<Solicitudinsumo> findSolicitudinsumoEntities(int maxResults, int firstResult) {
        return findSolicitudinsumoEntities(false, maxResults, firstResult);
    }

    private List<Solicitudinsumo> findSolicitudinsumoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Solicitudinsumo.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Solicitudinsumo findSolicitudinsumo(SolicitudinsumoPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Solicitudinsumo.class, id);
        } finally {
            em.close();
        }
    }

    public int getSolicitudinsumoCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Solicitudinsumo> rt = cq.from(Solicitudinsumo.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
